package com.youlu.campus.web.platform;

import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.PlatformUserComplaint;
import com.youlu.campus.entity.VO.req.PlatformUserComplaintQueryVO;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.platform.PlatformUserComplaintService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Objects;

@Slf4j
@RestController
public class PlatformUserComplaintController {
    @Autowired
    private PlatformUserComplaintService platformUserComplaintService;
    @Autowired
    private ActivityService activityService;

    /**
     * 服务记录
     *
     * @return
     */
    @PostMapping("/mini-app/platform/complaint/list")
    public QResult list(@RequestBody @NonNull PlatformUserComplaintQueryVO req) {
        req.setPlatform(RequestContext.getCurPlatform());
        req.setUserId(RequestContext.getUserId());
        req.setCreateType("system");
        return QResult.success(platformUserComplaintService.list(req));
    }

    /**
     * 投诉详情
     *
     * @return
     */
    @GetMapping("/mini-app/platform/complaint/find-one")
    public QResult findOne(@RequestParam("id") String id) {
        return QResult.success(platformUserComplaintService.findOne(id));
    }

    /**
     * 平台投诉
     *
     * @return
     */
    @PostMapping("/mini-app/platform/complaint/create")
    public QResult create(@RequestBody @NonNull PlatformUserComplaint req) {
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        req.setUserId(RequestContext.getUserId());
        req.setOpenId(RequestContext.getCurUser().getOpenId());
        req.setAvatarUrl(RequestContext.getCurUser().getAvatarUrl());
        if (StringUtils.isNotBlank(req.getActivityId())) {
            ActivityInfo a = activityService.getActivityInfo(req.getActivityId());
            if (Objects.nonNull(a)) {
                req.setActivityName(a.getName());
            }
        }
        return QResult.success(platformUserComplaintService.create(req));
    }

    /**
     * 平台投诉匿名接口
     *
     * @return
     */
    @PostMapping("/public/platform/complaint/create")
    public QResult publicCreate(@RequestBody @NonNull PlatformUserComplaint req) {
//        req.setPlatform(RequestContext.getCurPlatform());
//        req.setAppId(RequestContext.getAppId());
//        req.setUserId(RequestContext.getUserId());
//        req.setOpenId(RequestContext.getCurUser().getOpenId());
//        req.setAvatarUrl(RequestContext.getCurUser().getAvatarUrl());
        if (StringUtils.isNotBlank(req.getActivityId())) {
            ActivityInfo a = activityService.getActivityInfo(req.getActivityId());
            if (Objects.nonNull(a)) {
                req.setActivityName(a.getName());
            }
        }
        return QResult.success(platformUserComplaintService.create(req));
    }

    @GetMapping("/mini-app/platform/complaint/unread-msg")
    public QResult getUnreadMsg() {
        Long count = platformUserComplaintService.unReadMsgCount(RequestContext.getUserId());
        HashMap<String, Object> res = new HashMap<>();
        if (count == null || count.compareTo(0L) != 1) {
            res.put("hasMsg", Boolean.FALSE);
            res.put("count", 0L);
        } else {
            res.put("hasMsg", Boolean.TRUE);
            res.put("count", count);
        }
        return QResult.success(res);
    }

}
